package com.colabo.j2ee.web.oaf.menu.action;

import java.util.List;

import net.sf.json.JSONArray;

import com.colabo.j2ee.web.core.cache.CacheManager;
import com.colabo.j2ee.web.core.context.ContextUtil;
import com.colabo.j2ee.web.oaf.menu.CachedMenuBean;
import com.colabo.j2ee.web.oaf.menu.IMenuService;


public class MenuAction {
	
	IMenuService menuService = (IMenuService) ContextUtil.getBean(IMenuService.BEAN_ID);
	private static CacheManager cache = new CacheManager();
	
	public MenuAction(){
		super();
	}
	
	public String getAllMenus() throws Exception{
		
		String ret = "";
		ret = getAllMenusFromCache();
		if (ret == null || ret.length() < 1) {
			return getAllMenusFromPersistence();
		}
		return ret;
	}
	
	private String getAllMenusFromCache() {
		String ret = cache.getObj(CachedMenuBean.ALLMENUS).toString();
		return ret;
	}
	
	public String getAllMenusFromPersistence() throws Exception {
		String ret = "";
		List menus = menuService.getAllMenus();
		JSONArray json = JSONArray.fromObject(menus);
		ret = json.toString();
		return ret;
	}
}
